<?php
/**
 * 新人特惠管理
 *
 *
 *
 *
 * @copyright  Copyright (c) 中国追溯
 * @since      File available since Release v1.1
 */

defined('InShopNC') or exit('Access Invalid!');

class p_newbieModel extends Model {
    const STATE1 = 1;       // 开启
    const STATE0 = 0;       // 关闭
    
    public function __construct() {
        parent::__construct();
    }

    /**
     * 新人特惠套餐列表
     *
     * @param array $condition
     * @param string $field
     * @param int $page
     * @param string $order
     * @return array
     */
    public function getNewbieQuotaList($condition, $field = '*', $page = 0, $order = 'newbie_quota_id desc') {
        return $this->table('p_newbie_quota')->field($field)->where($condition)->order($order)->page($page)->select();
    }

    /**
     * 新人特惠套餐详细信息
     *
     * @param array $condition
     * @param string $field
     * @return array
     */
    public function getNewbieQuotaInfo($condition, $field = '*') {
        return $this->table('p_newbie_quota')->field($field)->where($condition)->find();
    }

    /**
     * 通过的套餐详细信息
     *
     * @param int $store_id
     * @param string $field
     * @return array
     */
    public function getNewbieQuotaInfoCurrent($store_id) {
        $condition['store_id'] = $store_id;
        $condition['newbie_quota_endtime'] = array('gt', TIMESTAMP);
        $condition['newbie_state'] = self::STATE1;
        return $this->getNewbieQuotaInfo($condition);
    }

    /**
     * 保存新人特惠套餐
     *
     * @param array $insert
     * @param boolean $replace
     * @return boolean
     */
    public function addNewbieQuota($insert, $replace = false) {
        return $this->table('p_newbie_quota')->pk(array('newbie_quota_id'))->insert($insert, $replace);
    }

    /**
     * 编辑新人特惠套餐
     * @param array $update
     * @param array $condition
     * @return array
     */
    public function editNewbieQuota($update, $condition) {
        return $this->table('p_newbie_quota')->where($condition)->update($update);
    }

    /**
     * 编辑新人特惠套餐
     * @param array $update
     * @param array $condition
     * @return array
     */
    public function editNewbieQuotaOpen($update, $condition) {
        $update['newbie_state'] = self::STATE1;
        return $this->table('p_newbie_quota')->where($condition)->update($update);
    }

    /**
     * 商品列表
     *
     * @param array $condition
     * @param string $field
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return array
     */
    public function getNewbieGoodsList($condition, $field = '*', $page = 0, $limit = 0, $order = 'newbie_goods_id asc') {
        return $this->table('p_newbie_goods')->field($field)->where($condition)->limit($limit)->order($order)->page($page)->select();
    }

    /**
     * 获取新人特惠商品详细信息
     * @param array $condition
     * @param string $field
     * @return array
     */
    public function getNewbieGoodsInfo($condition, $field = '*') {
        return $this->table('p_newbie_goods')->field($field)->where($condition)->find();
    }

    /**
     * 取得商品信息
     * @param int $goods_id
     * @return array
     */
    public function getNewbieGoodsInfoOpenByGoodsID($goods_id) {
        $goods_info = $this->getNewbieGoodsInfo(array('goods_id'=>$goods_id, 'newbie_state' => self::STATE1));
        return $goods_info;
    }

    /**
     * 保存新人特惠商品信息
     * @param array $insert
     * @return boolean
     */
    public function addNewbieGoods($insert) {
        return $this->table('p_newbie_goods')->insert($insert);
    }
    
    /**
     * 更新新人特惠商品信息
     */
    public function editNewbieGoods($update, $condition) {
        $newbie_goods_list = $this->getNewbieGoodsList($condition);
        if (empty($newbie_goods_list)) {
            return true;
        }
        $goodsid_array = array();
        foreach ($newbie_goods_list as $val) {
            $goodsid_array[] = $val['goods_id'];
        }
        $result = $this->table('p_newbie_goods')->where(array('goods_id' => array('in', $goodsid_array)))->update($update);
        if ($result) {
            foreach ($goodsid_array as $val) {
                $this->_dGoodsNewbieCache($val);
            }
        }
        return $result;
    }

    /**
     * 更新套餐为关闭状态
     * @param array $condition
     * @return boolean
     */
    public function editNewbieClose($condition) {
        $quota_list = $this->getNewbieQuotaList($condition);
        if (empty($quota_list)) {
            return true;
        }
        $storeid_array = array();
        foreach ($quota_list as $val) {
            $storeid_array[] = $val['store_id'];
        }
        $where = array('store_id' => array('in', $storeid_array));
        $update = array('newbie_state' => self::STATE0);
        $this->editNewbieQuota($update, $where);
        $this->editNewbieGoods($update, $where);
        return true;
    }

    /**
     * 删除新人特惠商品
     *
     * @param unknown $condition
     * @return boolean
     */
    public function delNewbieGoods($condition) {
        return $this->table('p_newbie_goods')->where($condition)->delete();
    }

    /**
     * 读取商品限时折扣缓存
     * @param int $goods_id
     * @return array/bool
     */
    private function _rGoodsNewbieCache($goods_id) {
        return rcache($goods_id, 'goods_newbie');
    }
    
    /**
     * 写入商品限时折扣缓存
     * @param int $goods_id
     * @param array $info
     * @return boolean
     */
    private function _wGoodsNewbieCache($goods_id, $info) {
        return wcache($goods_id, $info, 'goods_newbie');
    }
    
    /**
     * 删除商品限时折扣缓存
     * @param int $goods_id
     * @return boolean
     */
    private function _dGoodsNewbieCache($goods_id) {
        return dcache($goods_id, 'goods_newbie');
    }
}
